# -*- coding: utf-8 -*-
# article_info.py
# Created by Hardy on 26th, Jan
# Copyright 2017 杭州网川教育有限公司. All rights reserved.

import json

from querier.esquerier import ElasticSearchQuerier
import happybase


class ArticleHBASEQuerier(ElasticSearchQuerier):

    def __init__(self, host, table):
        super(ArticleHBASEQuerier, self).__init__(None, None, None)
        self.host = host
        self.table = table

    def search(self, args):
        article_id = args.get('article_id')
        if article_id is None:
            raise ValueError('message: "article_id" is needed')

        hbase_connection = happybase.Connection(self.host)
        hbase_table = hbase_connection.table(bytes(self.table, encoding='utf-8'))

        article = hbase_table.row(bytes(article_id, encoding='utf-8'))
        result = json.loads(article.get(b'0:article', b'').decode())
        hbase_connection.close()
        return result

    def _build_query(self, args): pass

    def _build_result(self, result, param): pass
